package com.keke.myblogv1.users.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.keke.myblogv1.common.util.JwtUtil;
import com.keke.myblogv1.users.entity.MyBlogUsers;
import com.keke.myblogv1.users.mapper.MyBlogUsersMapper;
import com.keke.myblogv1.users.service.MyBlogUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keke.myblogv1.users.vo.UserLoginVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Derrick
 * @since 2022-08-02
 */
@Service
public class MyBlogUsersServiceImpl extends ServiceImpl<MyBlogUsersMapper, MyBlogUsers> implements MyBlogUsersService {

    @Resource
    private MyBlogUsersMapper myBlogUsersMapper;

    @Override
    public UserLoginVO getLoginUser(MyBlogUsers myBlogUsers) {
        LambdaQueryWrapper<MyBlogUsers> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MyBlogUsers::getUsername,myBlogUsers.getUsername())
                .eq(MyBlogUsers::getPassword,myBlogUsers.getPassword());
        MyBlogUsers user = myBlogUsersMapper.selectOne(queryWrapper);
        String token = JwtUtil.sign(myBlogUsers.getUsername());
        UserLoginVO userLoginVO = new UserLoginVO();
        userLoginVO.setUserInfo(user);
        userLoginVO.setToken(token);
        return userLoginVO;
    }

    @Override
    public MyBlogUsers getUserInfo(String token) {
        if(JwtUtil.checkSign(token)){
            String userId = JwtUtil.getUserId(token);
            LambdaQueryWrapper<MyBlogUsers> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(MyBlogUsers::getUsername,userId);
            MyBlogUsers myBlogUsers = myBlogUsersMapper.selectOne(queryWrapper);
            return myBlogUsers;
        }
        return null;
    }

    @Override
    public void logout() {
        String token = JwtUtil.sign("admin");
    }
}
